#include "goalwindow.h"
#include "resultwindow.h"
#include "aichatdialog.h"
#include <QMainWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTextEdit>
#include <QMessageBox>

GoalWindow::GoalWindow(ResultWindow* resultWindow, QWidget *parent) : QMainWindow(parent), resultWindow(resultWindow)
{
    setWindowTitle("✨ 设定理想BMI目标 ✨");
    resize(600, 500);
    setStyleSheet("background-color: #f8f9fa;");

    QLabel *titleLabel = new QLabel("设定您的BMI目标", this);
    titleLabel->setStyleSheet(
        "font-size: 20pt; "
        "font-weight: bold; "
        "color: #6c757d; "
        "font-family: 'Microsoft YaHei';"
    );

    QLabel *targetLabel = new QLabel("🎯 目标BMI:", this);
    targetLabel->setStyleSheet("font-size: 14pt; color: #495057;");
    targetBmiInput = new QLineEdit(this);
    targetBmiInput->setPlaceholderText("请输入目标BMI值 (18.5-24为健康范围)");
    targetBmiInput->setStyleSheet(
        "QLineEdit {"
        "  font-size: 14pt;"
        "  padding: 10px;"
        "  border: 2px solid #dee2e6;"
        "  border-radius: 15px;"
        "  background-color: white;"
        "}"
    );

    QLabel *timeframeLabel = new QLabel("⏳ 期望实现期限(月):", this);
    timeframeLabel->setStyleSheet("font-size: 14pt; color: #495057;");
    timeframeInput = new QLineEdit(this);
    timeframeInput->setPlaceholderText("请输入期望几个月内达成");
    timeframeInput->setStyleSheet(
        "QLineEdit {"
        "  font-size: 14pt;"
        "  padding: 10px;"
        "  border: 2px solid #dee2e6;"
        "  border-radius: 15px;"
        "  background-color: white;"
        "}"
    );

    evaluateBtn = new QPushButton("✨ 评估可行性", this);
    evaluateBtn->setStyleSheet(
        "QPushButton {"
        "  font-size: 14pt;"
        "  padding: 12px 24px;"
        "  background-color: #4cc9f0;"
        "  color: white;"
        "  border-radius: 15px;"
        "  border: none;"
        "}"
        "QPushButton:hover {"
        "  background-color: #4895ef;"
        "}"
    );

    adviceText = new QTextEdit(this);
    adviceText->setReadOnly(true);
    adviceText->setStyleSheet(
        "QTextEdit {"
        "  font-size: 12pt;"
        "  padding: 10px;"
        "  border: 2px solid #dee2e6;"
        "  border-radius: 15px;"
        "  background-color: white;"
        "}"
    );

    connect(evaluateBtn, &QPushButton::clicked, [this, resultWindow]() {
        bool ok;
        double targetBmi = targetBmiInput->text().toDouble(&ok);
        if (!ok || targetBmi < 15 || targetBmi > 40) {
            QMessageBox::warning(this, "错误", "请输入有效的BMI值(15-40)");
            return;
        }

        int months = timeframeInput->text().toInt(&ok);
        if (!ok || months <= 0) {
            QMessageBox::warning(this, "错误", "请输入有效的月数");
            return;
        }

        // 获取最新BMI值
        double latestBmi = resultWindow ? resultWindow->getLatestBmi() : 0;
        double bmiChange = targetBmi - latestBmi;
        double monthlyChange = bmiChange / months;

        QString advice;
        if (fabs(monthlyChange) > 0.5) {
            advice = QString("您的目标过于激进(每月变化%1)。当前BMI(%2)与目标BMI(%3)的差值为%4。建议:\n")
                         .arg(monthlyChange, 0, 'f', 2)
                         .arg(latestBmi, 0, 'f', 1)
                         .arg(targetBmi, 0, 'f', 1)
                         .arg(bmiChange, 0, 'f', 1);
            advice += "- 延长实现期限\n";
            advice += "- 咨询专业医生\n";
        } else {
            advice = QString("您的目标可行(每月变化%1)。当前BMI(%2)与目标BMI(%3)的差值为%4。建议:\n")
                         .arg(monthlyChange, 0, 'f', 2)
                         .arg(latestBmi, 0, 'f', 1)
                         .arg(targetBmi, 0, 'f', 1)
                         .arg(bmiChange, 0, 'f', 1);
            if (bmiChange > 0) {
                advice += "- 需要增重\n";
                advice += "- 增加蛋白质摄入\n";
                advice += "- 进行力量训练\n";
            } else if (bmiChange < 0) {
                advice += "- 需要减重\n";
                advice += "- 控制饮食热量\n";
                advice += "- 增加有氧运动\n";
            } else {
                advice += "- 当前BMI已达到目标\n";
            }
            advice += "- 每月监测BMI变化\n";
        }

        adviceText->setText(advice);
    });

    backButton = new QPushButton("🔙 返回", this);
    backButton->setStyleSheet(
        "QPushButton {"
        "  font-size: 14pt;"
        "  padding: 12px 24px;"
        "  background-color: #adb5bd;"
        "  color: white;"
        "  border-radius: 15px;"
        "  border: none;"
        "}"
        "QPushButton:hover {"
        "  background-color: #6c757d;"
        "}"
        );

    aiChatButton = new QPushButton("🤖 AI详细咨询", this);
    aiChatButton->setStyleSheet(
        "QPushButton {"
        "  font-size: 14pt;"
        "  padding: 12px 24px;"
        "  background-color: #4cc9f0;"
        "  color: white;"
        "  border-radius: 15px;"
        "  border: none;"
        "}"
        "QPushButton:hover {"
        "  background-color: #4895ef;"
        "}"
    );

    aiChatDialog = new AIChatDialog(this);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(backButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(aiChatButton);
    buttonLayout->addWidget(evaluateBtn);

    connect(aiChatButton, &QPushButton::clicked, [this]() {
        aiChatDialog->setInitialPrompt(adviceText->toPlainText());
        aiChatDialog->show();
    });

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(titleLabel);
    layout->addWidget(targetLabel);
    layout->addWidget(targetBmiInput);
    layout->addWidget(timeframeLabel);
    layout->addWidget(timeframeInput);
    layout->addLayout(buttonLayout);
    layout->addWidget(adviceText);

    connect(backButton, &QPushButton::clicked, this, &GoalWindow::returnToPrevious);

    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);
}
